package com.wy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wy.common.utils.RouterHelper;
import com.wy.model.entity.Permission;
import com.wy.model.entity.RolePermission;
import com.wy.model.vo.AssignMenuVo;
import com.wy.model.vo.RouterVo;
import com.wy.system.exception.MyException;
import com.wy.system.mapper.PermissionMapper;
import com.wy.system.mapper.RolePermissionMapper;
import com.wy.system.service.PermissionService;
import com.wy.system.utils.PermissionHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

  @Autowired
  private RolePermissionMapper rolePermissionMapper;

  @Override
  public List<Permission> findNodes() {
    //获取所有菜单
    List<Permission> permissionList = baseMapper.selectList(null);
    //所有菜单数据转换要求的数据格式
    List<Permission> resultList = PermissionHelper.buildTree(permissionList);
    return resultList;
  }

  //删除菜单
  @Override
  public void removePermissionById(Integer id) {
    //查询当前删除菜单下面是否有子菜单

    //根据id == parentid进行删除
    QueryWrapper<Permission> wrapper = new QueryWrapper<>();
    wrapper.eq("parent_id",id);
    Integer count = baseMapper.selectCount(wrapper);
    if (count > 0){
      //有子菜单
      throw  new MyException(201,"请先删除子菜单");
    }
    //调用删除
    baseMapper.deleteById(id);
  }

  //根据角色分配菜单
  @Override
  public List<Permission> findMenuByRoleId(Integer roleId) {
    //获取所有的菜单status=1
    QueryWrapper<Permission> wrapperMenu = new QueryWrapper<>();
    wrapperMenu.eq("status",1);
    List<Permission> menuList = baseMapper.selectList(wrapperMenu);
    //根据角色id查询 角色分配过的菜单列表
    QueryWrapper<RolePermission> wrapperRolePermission = new QueryWrapper<>();
    wrapperRolePermission.eq("role_id",roleId);
    List<RolePermission> rolePermissions = rolePermissionMapper.selectList(wrapperRolePermission);
    //从第二部查询的列表中，获取角色分配所有菜单id
    List<Integer> rolePermissionIds = new ArrayList<>();
    for (RolePermission rolePermission :
        rolePermissions) {
      int permissionid = rolePermission.getPermissionId();
      rolePermissionIds.add(permissionid);
    }
    //数据处理：isSelect，如果菜单被选中则为true，否则为false
    //拿着分配菜单id 和 所有菜单比对，有相同的，让isSelect值为true
    for (Permission permission: menuList) {
      if (rolePermissionIds.contains(permission.getId())){
        permission.setSelect(true);
      }else {
        permission.setSelect(false);
      }
    }
    //转换成树形结构为了最终显示MenuHelper方法实现
    List<Permission> permissions = PermissionHelper.buildTree(menuList);
    return permissions;
  }


  //给角色分配菜单权限
  @Override
  public void doAssign(AssignMenuVo assignMenuVo) {
    //根据角色id删除菜单权限
    QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
    wrapper.eq("role_id",assignMenuVo.getRoleId());
    rolePermissionMapper.delete(wrapper);
    //遍历菜单id列表，一个一个进行添加
    List<Integer> menuIdList = assignMenuVo.getMenuIdList();
    for (Integer menuId : menuIdList) {
      RolePermission rolePermission = new RolePermission();
      rolePermission.setPermissionId(menuId);
      rolePermission.setRoleId(assignMenuVo.getRoleId());
      rolePermissionMapper.insert(rolePermission);
    }
  }

  //根据userid查询菜单权限值
  @Override
  public List<RouterVo> getUserMenuList(Integer userId) {
    List<Permission> permissionList = null;
    //判断userId的值是1代表超级管理员，查询所有权限数据
    if (userId.equals(1)){
      QueryWrapper<Permission> wrapper = new QueryWrapper<>();
      wrapper.eq("status",1);
      wrapper.orderByAsc("sort_value");
      permissionList = baseMapper.selectList(wrapper);
    } else {
      //如果userid不是1，则为其他用户类型，查询这个用户权限
      permissionList = baseMapper.findPermissionListUserId(userId);
    }
    //构建树形结构
    List<Permission> permissionTreeList = PermissionHelper.buildTree(permissionList);
    //转换成前端路由要求的格式数据
    List<RouterVo> routerVoList = RouterHelper.buildRouters(permissionTreeList);
    return routerVoList;
  }

  //根据userid查询按钮权限值
  @Override
  public List<String> getUserButtonList(Integer userId) {
    List<Permission> permissionList = null;
    //判断是否是管理员
    if (userId.equals(1)){
       permissionList = baseMapper.selectList(new QueryWrapper<Permission>().eq("status", 1));
    }else {
      permissionList = baseMapper.findPermissionListUserId(userId);
    }
    //遍历permissionList
    List<String> buttonList = new ArrayList<>();
    for (Permission permission: permissionList) {
      //type==2即为按钮
      if (permission.getType()==2){
        String perms = permission.getPerms();
        buttonList.add(perms);
      }
    }
    return buttonList;
  }

}
